﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Website.TaskService;
using MobileSales.Model.Tasks;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class CyclicTasksController : ControllerBase
    {
        #region Constants
        public const string CyclicTaskDateSessionKey = "cyclicTasks_selectedDate";
        #endregion

        #region Index
        //
        // GET: /CyclicTasks/

        public ActionResult Index(int? employeeId, DateTime? date)
        {
            if (date.HasValue)
                Session[CyclicTaskDateSessionKey] = date.Value;
            else
                Session[CyclicTaskDateSessionKey] = DateTime.Now;

            if (employeeId.HasValue)
            {
                IEnumerable<CyclicTask> tasks = null;
                try
                {
                    TaskServiceClient taskService = new TaskServiceClient();
                    taskService.AddValidationToken(this.Token);
                    tasks = taskService.GetCyclicTaskList(employeeId.Value);
                    ViewData["employeeId"] = employeeId;
                    taskService.Close();
                }
                catch (Exception ex)
                {
                    CLogger.LogDebug(ex);
                    this.SetMessage(ErrorStrings.ErrorTaskList, MessageType.Error);
                }
                return View(tasks);
            }
            return View();
        } 
        #endregion

        #region Details
        //
        // GET: /CyclicTasks/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                CyclicTask task = client.GetCyclicTask(id);
                client.Close();
                return View(task);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.TaskNotFound, MessageType.Error);
                return RedirectToAction("Index");
            }
        } 
        #endregion

        #region Create
        //
        // GET: /CyclicTasks/Create

        public ActionResult Create(int? employeeId)
        {
            ViewData["employeeId"] = employeeId;
            return View();
        }

        //
        // POST: /CyclicTasks/Create

        [HttpPost]
        public ActionResult Create(int taskTypeId, int taskStatusId, CyclicTask task)
        {
            try
            {
                task.TaskStatus = new TaskStatus { Id = taskStatusId };
                task.TaskType = new TaskType { Id = taskTypeId };
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.StoreCyclicTask(task);
                client.Close();
                this.SetMessage(InfoStrings.TaskAdded, MessageType.Info);
                return RedirectToAction("Index", new { employeeId = task.EmployeeId });
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskAdding, MessageType.Error);

            }
            return View(task);
        } 
        #endregion

        #region Edit
        //
        // GET: /CyclicTasks/Edit/5

        public ActionResult Edit(int id)
        {
            return Details(id);
        }

        //
        // POST: /CyclicTasks/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, int taskTypeId, int taskStatusId, CyclicTask task)
        {
            try
            {
                task.Id = id;
                task.TaskStatus = new TaskStatus { Id = taskStatusId };
                task.TaskType = new TaskType { Id = taskTypeId };
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.StoreCyclicTask(task);
                client.Close();
                this.SetMessage(InfoStrings.TaskUpdated, MessageType.Info);
                return RedirectToAction("Index", new { employeeId = task.EmployeeId });
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskUpdating, MessageType.Error);

            }
            return View(task);
        } 
        #endregion

        #region Delete
        //
        // GET: /CyclicTasks/Delete/5

        public ActionResult Delete(int id, int employeeId)
        {
            ViewData["employeeId"] = employeeId;
            return Details(id);
        }

        //
        // POST: /CyclicTasks/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, int employeeId, FormCollection collection)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.DeleteTask(id);
                client.Close();
                this.SetMessage(InfoStrings.TaskDeleted, MessageType.Info);
                return RedirectToAction("Index", new { employeeId = employeeId });
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskDeleting, MessageType.Error);
            }
            return RedirectToAction("Index", new { employeeId = employeeId });
        } 
        #endregion
    }
}
